package group2assignone.view.manager;

import group2assignone.model.Flight;
import group2assignone.view.OurView;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 *This class handles the GUI for the 'Create Flight' view when a user
 * selects to create a flight.
 * @author group2
 */
public class CreateFlightView extends OurView {

    private JButton next = new JButton("Next");
    private JButton create = new JButton("Create");
    private JButton edit = new JButton("Edit");
    private JButton cancel = new JButton("Cancel");
    private JLabel fDetailsTitle = new JLabel("Please enter flight details:");
    private JLabel confirmTitle = new JLabel("Create flight:");
    private JLabel flightNumPrompt = new JLabel("Flight Number:");
    private JLabel depLocPrompt = new JLabel("Departure Location:");
    private JLabel depTimePrompt = new JLabel("Departure Time:");
    private JLabel arrLocPrompt = new JLabel("Arrival Location:");
    private JLabel arrTimePrompt = new JLabel("Arrival Time:");
    private JLabel seatCapPrompt = new JLabel("Seating Capacity: ");
    private JLabel datePrompt = new JLabel("Date: ");
    private JLabel costPrompt = new JLabel("Ticket Price: ");
    private JTextField flightNum = new JTextField(5);
    private JTextField depLoc = new JTextField(10);
    private JTextField depTime = new JTextField(8);
    private JTextField arrLoc = new JTextField(10);
    private JTextField arrTime = new JTextField(8);
    private JTextField seatCap = new JTextField(3);
    private JTextField date = new JTextField(8);
    private JTextField cost = new JTextField(5);
    private JLabel newFlightNum = new JLabel();
    private JLabel newDepLoc = new JLabel();
    private JLabel newDepTime = new JLabel();
    private JLabel newArrLoc = new JLabel();
    private JLabel newArrTime = new JLabel();
    private JLabel newSeatCap = new JLabel();
    private JLabel newDate = new JLabel();
    private JLabel newCost = new JLabel();

    /**
     *Constructor.
     * Performs no function.
     */
    public CreateFlightView() {
    }

    /**
     * Adds all of the relevant components to the view.
     * Created view will allow flight data to be entered.
     */
    public void addComponents() {
        westPanel.setLayout(new GridLayout(4, 2, 7, 2));
        eastPanel.setLayout(new GridLayout(4, 2, 7, 2));

        northPanel.add(fDetailsTitle);

        westPanel.add(flightNumPrompt);
        westPanel.add(flightNum);
        westPanel.add(depLocPrompt);
        westPanel.add(depLoc);
        westPanel.add(arrLocPrompt);
        westPanel.add(arrLoc);
        westPanel.add(seatCapPrompt);
        westPanel.add(seatCap);

        eastPanel.add(datePrompt);
        eastPanel.add(date);
        eastPanel.add(depTimePrompt);
        eastPanel.add(depTime);
        eastPanel.add(arrTimePrompt);
        eastPanel.add(arrTime);
        eastPanel.add(costPrompt);
        eastPanel.add(cost);

        eastPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 10));
        westPanel.setBorder(BorderFactory.createEmptyBorder(2, 10, 2, 2));

        southPanel.add(next);
        southPanel.add(cancel);

        repaint();
    }

    /*
     * Gets relevant flight data from Flight paramater f and loads it
     * into appropriate text fields.
     * @param f
     */
    public void setText(Flight f) {
        flightNum.setText(f.getFlightNum());
        arrLoc.setText(f.getArrLoc());
        depLoc.setText(f.getDepLoc());
        arrTime.setText(f.getArrTime());
        depTime.setText(f.getDepTime());
        seatCap.setText("" + f.getSeatCap());
        date.setText(f.getDate());
        cost.setText(f.getCost());
    }

    /**
     * Removes all components from border layout panels.
     */
    public void clearScreen() {
        northPanel.removeAll();
        westPanel.removeAll();
        eastPanel.removeAll();
        southPanel.removeAll();
    }

    /**
     * Sets action listener for next button.
     * @param ActionListener cr
     */
    public void setCreateAL(ActionListener cr) {
        next.addActionListener(cr);
    }

    /**
     * Sets action listener for edit button.
     * @param ActionListener ed
     */
    public void setEditAL(ActionListener ed) {
        edit.addActionListener(ed);
    }

    /**
     * Sets action listener for create button.
     * @param ActionListener d
     */
    public void setDoneAL(ActionListener d) {
        create.addActionListener(d);
    }

    /**
     * Sets action listener for cancel button.
     * @param ActionListener ca
     */
    public void setCancelAL(ActionListener ca) {
        cancel.addActionListener(ca);
    }

    /*
     * Loads details in text fields into Flight f object.
     * @param Flight f
     */
    public void loadDetails(Flight f) {
        f.setFlightNum(flightNum.getText());
        f.setArrLoc(arrLoc.getText());
        f.setArrTime(arrTime.getText());
        f.setDepLoc(depLoc.getText());
        f.setDepTime(depTime.getText());
        if (!seatCap.getText().equals("")) {
            f.setSeatCap(Integer.parseInt((seatCap.getText())));
        }
        f.setDate(date.getText());
        f.setCost(cost.getText());
    }

    /**
     * Shows flight details as labels.  New flight details are obtained from
     * Flight f.
     * @param Flight f
     */
    public void showNewFlight(Flight f) {
        clearScreen();
        northPanel.add(confirmTitle);

        westPanel.add(flightNumPrompt);
        westPanel.add(newFlightNum);
        westPanel.add(depLocPrompt);
        westPanel.add(newDepLoc);
        westPanel.add(arrLocPrompt);
        westPanel.add(newArrLoc);
        westPanel.add(seatCapPrompt);
        westPanel.add(newSeatCap);

        eastPanel.add(datePrompt);
        eastPanel.add(newDate);
        eastPanel.add(depTimePrompt);
        eastPanel.add(newDepTime);
        eastPanel.add(arrTimePrompt);
        eastPanel.add(newArrTime);
        eastPanel.add(costPrompt);
        eastPanel.add(newCost);

        southPanel.add(create);
        southPanel.add(edit);

        newFlightNum.setText(f.getFlightNum());
        newArrLoc.setText(f.getArrLoc());
        newDepLoc.setText(f.getDepLoc());
        newArrTime.setText(f.getArrTime());
        newDepTime.setText(f.getDepTime());
        newSeatCap.setText("" + f.getSeatCap());
        newDate.setText(f.getDate());
        newCost.setText(f.getCost());
        repaint();
    }
}

